import { defineStore } from "pinia";
import { parse, stringify } from "zipson";

export const useUserStore = defineStore(
  "user",
  () => {
    // 登录状态
    let token = ref("");
    // 用户信息
    let userInfo = ref({});

    function setUserInfo({ _token, user, perms }) {
      token.value = _token;
      userInfo.value = user;
    }

    function resetToken() {
      token.value = "";
      userInfo.value = {};
    }

    return {
      token,
      userInfo,
      setUserInfo,
      resetToken,
    };
  },
  {
    persist: {
      paths: ["token", "userInfo"],
      serializer: {
        deserialize: parse,
        serialize: stringify,
      },
    },
  }
);
